import { Component, OnInit , ElementRef, Renderer2, ViewChild, } from '@angular/core';
import { TipService, ModalTipService, ModalService ,HttpService} from 'itcloud-plugin';
import { GetPathService } from '../../get-path.service';

@Component({
  selector: 'app-add-sendee',
  templateUrl: './add-sendee.component.html',
  styleUrls: ['./add-sendee.component.css']
})
export class AddSendeeComponent implements OnInit {
  data;
  handler;            //来自父组件的数据，和方法；
  name:string;         
  telephone:string;
  //下拉框
  selectId;
  treeParams1;

    //tablenew 表格；
    // 表头数据
    headerConfig=[{field: 'userName',title: '姓名',width: '20%',}, {field: 'phone',title: '手机号',width: '20%',}, {field: 'email',title: '邮箱',width: '60%',}];
    // 表内容数据
    tableData: Array<any>;  //所展示的列表内容；
    tableDataAll: Array<any> = [];  //说有的的列表内容；
    addTableData: Array<any> = [];  //添加的列表内容；
 
    // tableId: string;
    tableLoading: boolean;
    tableOptions = {
      // maxHeight: '200px',
      // minHeight: '100px',
      multiple:true,      //多选checkbox；
      checkbox: true,
      loading: true,
      // showIndex: true
    };

     //分页功能；
     pager3 = {
       total: 0,
       limit: 5,
       page: 1,
     };

     
  @ViewChild('tableNew') tableNew;
  @ViewChild('selectTree2') selectTree2;

  constructor(
    private renderer2: Renderer2,
    private http: HttpService,
    private GetPathService: GetPathService,
    private tipService: TipService,
  ) { }

  ngOnInit() {
    this.getSelect()    //获取下拉框数据；
    this.getList();
 
  }

  getSelect(){
    const param = {
    };
    this.http.getData({
      url: this.GetPathService.AjaxPath.bulletinServer+'/bulletin/organLsit',
      search: param,
    }).subscribe(res => {
      this.treeParams1 = {
        treeDatas: res['beans'],
        keyId: 'id',
        keyName: 'name',
        keyChild: 'children',
        options: {
          paddingLeft: '20',
          unSelect: true,
        }
      };
    }, err => { });

  }
  //下拉框发生变化；
  nodeClick(even){
    if(even){
    console.log(even)
    this.selectId= even.id;
    console.log(this.selectId)
    }else{
    this.selectId='';
    console.log(this.selectId)
    }
  }


 
  reset(){
    this.name='';
    this.telephone='';
    this.selectTree2.currentValue='';
    this.selectId='';
    this.getSelect()    //获取下拉框数据；
    this.getList();
  }

  checkEvent(e){
    this.addTableData=e.checked;
    console.log(e,this.addTableData);
  }

getList(){
  this.tableNew.allCheck.checked=false; //去掉全选按钮样式；
  this.addTableData=[];                 //清空添加的列表内容；
  const param = {
    userName:this.name,
    phone:this.telephone,
    organId:this.selectId
  };
  this.http.getData({
    url: this.GetPathService.AjaxPath.bulletinServer+'/bulletin/findUser',
    search: param,
  }).subscribe(res => {
    this.tableOptions.loading=false;
    this.tableDataAll= res['beans'] &&  res['beans'].list;
         //计算从tableDataAll数据中 截取当前页数来展示；
     this.tableData=this.tableDataAll.slice( this.pager3.limit*(this.pager3.page-1), this.pager3.limit*(this.pager3.page));
     this.pager3.total=this.tableDataAll.length;
     this.pager3.page=1;
  }, err => {this.tableOptions.loading=false; });
};
  //改变页数；
nowPageChange(pageNum) {
    this.tableNew.allCheck.checked=false; //去掉全选按钮样式；
    this.pager3.page=pageNum;
      //计算从tableDataAll数据中 截取当前页数来展示；
    this.tableData=this.tableDataAll.slice( this.pager3.limit*(this.pager3.page-1), this.pager3.limit*(this.pager3.page));
    
};
//选中全部按钮；
getAll(){
  if(this.tableDataAll.length){
    this.handler(this.tableDataAll);
  }else{
    this.tipService.show({ type: 'error', title:'没有获得数据'});
  }
};
add(){
  if(this.addTableData.length){
    this.handler(this.addTableData);
  }else{
    this.tipService.show({ type: 'error', title:'请添加至少一项'});
  }
}

cancel(){
  this.handler();
}

}
